from google.appengine.ext import db
import string
import datetime
import md5
import urllib
from BeautifulSoup import BeautifulSoup

import xmw  

class Post(db.Model):
    author=db.StringProperty()
    date=db.DateTimeProperty( )
    floor=db.StringProperty()
    content=db.TextProperty()    

    url=db.StringProperty()
    
    #post_id=db.StringProperty(multiline=False, required=True,default=xmw.get_uuid_hex())
    post_id=db.StringProperty(multiline=False, required=True)
    is_readed=db.BooleanProperty( required=True,default=False)
    
class Bot_Thread(db.Model):
    title=db.StringProperty(required=True)    
    #uuid
    #thread_id=db.StringProperty(multiline=False, required=True)        
    created =db.DateTimeProperty(auto_now_add=True)
    
    page_url=db.StringListProperty()
    
    last_scan=db.DateTimeProperty(required=True)
    
    latest_post_date=db.DateTimeProperty()
    
    type=db.StringProperty(multiline=False, required=True)  #LINK or FORM
    
    page_cnt=db.IntegerProperty(required=True)    # if FORM page,it means form counts
    
    status=db.StringProperty()
    chrAuthor=db.StringProperty()
    is_readed   =db.BooleanProperty(required=True,default=False)

class Post_Update_Request(db.Model):
    url =db.StringProperty(multiline=False, required=True)
    latest=db.DateTimeProperty(required=True)
    #thread_id =db.StringProperty(multiline=False, required=True)
    accept  =db.DateTimeProperty(auto_now_add=True)
    #if True,means we can find it in Progress
    is_in_progress=db.BooleanProperty(default=False)
    is_finished =db.BooleanProperty(default=False)

class Progress(db.Model):
    thread_id   =db.StringProperty(multiline=False, required=True)
    setp    =db.IntegerProperty(default=1)
    is_finished =db.BooleanProperty(default=False)    
        
class Page(db.Model):
    url =db.StringProperty(multiline=False, required=True)
    title   =db.StringProperty(multiline=False, required=True)
    seq =db.IntegerProperty(required=True)
    sibling =db.StringListProperty()
    lastPostDate    =db.DateTimeProperty()

class ThreadRequest(db.Model):
    seq =    db.IntegerProperty(required=True)
    #thread_id =db.StringProperty(multiline=False, required=True)
    url =db.StringProperty(multiline=False, required=True)
    latest=db.DateTimeProperty(required=True)
    accepted   =db.BooleanProperty(required=True,default=False)

class Task_ScanPost_Status:
    WAIT='WAIT'
    FINISHED='FINISHED'
    WORK='WORK'
    
class Task_ScanPost(db.Model):
    #the following 2 should be assigned when initialized
    type=db.StringProperty(multiline=False, required=True)
    page_url=db.StringListProperty(required=True)
    page_cnt=db.IntegerProperty(required=True)
    
    task_id =db.StringProperty(multiline=False, required=True,default=xmw.get_uuid_hex())
    #for FORM page to post.useless if its LINK type. 
    offset  =db.IntegerProperty(required=True,default=0)    
    created =db.DateTimeProperty(auto_now_add=True)
    #is_locked=db.BooleanProperty(required=True,default=False)
    status=db.StringProperty(multiline=False, required=True,default=Task_ScanPost_Status.WAIT)
    
    
"""
class LinkThread(db.Model):
    type=db.StringProperty(multiline=False, required=True)  #LINK or FORM
    offset=db.IntegerProperty(required=True)    #
"""

class URL(db.Model):
    url =db.StringProperty(multiline=False, required=True)
    scaned=db.BooleanProperty()

def get_url():
    q = URL.all()
    q.filter("scaned = ", False)        
    url = q.get()
    return url 

def put_url(url):
    url_tmp=URL(url=url,scaned=False)
    url_tmp.put()


        

def get_chrAuthor(soup):
    author_input=soup.find('input', {'name':'chrAuthor'})
    chrAuthor=author_input['value']
    #print chrAuthor
    return chrAuthor

def get_rs_strTitle_aa(soup):
    rs_strTitle_aa = soup.find('input', {'name':'rs_strTitle_aa'})

    return rs_strTitle_aa['value']


def get_intLogo(soup):
    intLogo = soup.find('input', {'name':'intLogo'})
    return intLogo['value']
class PAGE_TYPE:
    LINK = "LINK"
    FORM = "FORM"
    
def get_page_type(soup):
    #lst_pageForm=soup.find('form',{'id':'pageForm'})
    lst_pageForm = soup.find('form', id='pageForm')
    
    if (lst_pageForm is not None):
        return PAGE_TYPE.FORM 
    else:
        return PAGE_TYPE.LINK

def get_form_cnt(soup):
    
    apn = soup.find('input', {'name':'apn'})
    lst_pages = string.split(apn['value'], ',')
    #print len(lst_pages)
    return len(lst_pages)
    
def get_rs_permission(soup):
    rs_permission = soup.find ('input', {'name':'rs_permission'})
    return rs_permission['value'] 

def get_apn(soup):
    apn = soup.find('input', {'name':'apn'})
    return apn['value']

def get_title(soup):
    return get_rs_strTitle_aa(soup)
def walk_to_last_form(soup,url):
    """
    params = urllib.urlencode({'rs_strTitle_aa':rs_strTitle_aa_value,\
                               'intLogo':intLogo_value,\
                               'rs_permission':rs_permission_value,\
                               'apn':apn_value,\
                               'pID':pID_value
                               })
    """
    rs_strTitle_aa = get_rs_strTitle_aa(soup)
    intLogo = get_intLogo(soup)
    rs_permission = get_rs_permission(soup)
    apn = get_apn(soup)
    
    cnt = get_form_cnt(soup)
    
    str_cnt = "" + str(cnt)
    """
    header={"Content-type": "application/x-www-form-urlencoded",\
                               "Accept": "text/xml",\
                               "HOST":"www.tianya.cn"}
    """
    params = urllib.urlencode({
                               'rs_strTitle_aa':repr(rs_strTitle_aa),\
                               'intLogo':intLogo,\
                               'rs_permission':rs_permission,\
                               'apn':apn,\
                               'pID':str_cnt
                               })
    
    #page = urllib.urlopen(url, params,header)
    page = urllib.urlopen(url, params)
    #html = sock.read()
    soup = BeautifulSoup(page, fromEncoding="GB18030")
    page.close()
    
    return soup

def unicode_to_datetime(unicode_str):
    asc_str=unicode_str.encode('ascii')
    return ascii_to_datetime(asc_str)

def ascii_to_datetime(ascii_str):
    return datetime.datetime.strptime(ascii_str,'%Y-%m-%d %H:%M:%S')

def get_last_post_date(soup):
    
    lst_table=soup.findAll('table',{'align':'center','width':'100%'})
    t=lst_table.pop()    
    tr=t.tr
    td=tr.contents[3]
    c=td.contents
    s=c[0]

    d=s.contents[2]
    ss=d.split(u"\uff1a")
    post_date=unicode_to_datetime(ss[1])
    return post_date 

def get_md5(str):
    print type(str)
    m = md5.new()
    m.update(repr(str))
    m.digest()
    return m.hexdigest()

def now():
    return datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")    
def main():
      
    print xmw.get_uuid_hex()

if __name__ == '__main__':
    main()            